﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;


namespace TTMain.Settings
{
    /// <summary>
    /// Class which hold and manipulates the settings for the Thurn and Taxis game
    /// </summary>
    public class GameSettings
    {
        private String _nickname, _serverIP;
        private Int32 _serverPort, _localPort;
        private String fileName = "settings.xml";

        // The tags found in the settings xml file
        private const String TAG_ROOT = "settings";
        private const String TAG_NICKNAME = "nickname";
        private const String TAG_SERVER_IP = "serverIP";
        private const String TAG_SERVER_PORT = "serverPort";
        private const String TAG_LOCAL_PORT = "localPort";

        /// <summary>
        /// Creates a new game settings object and load the information from the default file if this exists
        /// </summary>
        public GameSettings()
        {
            _nickname = "Player";
            _serverIP = "0.0.0.0";
            _serverPort = 13000;

            LoadGameSettings();

        }
                
        /// <summary>
        /// Sets or gets the nickname 
        /// </summary>
        public String Nickname
        {

            get
            {
                return _nickname;
            }
            set
            {
                _nickname = value;
            }
        }


        /// <summary>
        /// Sets or gets the server IP
        /// </summary>
        public String ServerIP
        {

            get
            {
                return _serverIP;
            }
            set
            {
                _serverIP = value;
            }
        }

        /// <summary>
        /// Sets or gets the server Port
        /// </summary>
        public Int32 ServerPort
        {

            get
            {
                return _serverPort;
            }
            set
            {
                _serverPort = value;
            }
        }

        /// <summary>
        /// Sets or gets the local Port
        /// </summary>
        public Int32 LocalPort
        {

            get
            {
                return _localPort;
            }
            set
            {
                _localPort = value;
            }
        }

        /// <summary>
        /// Loads the settings from the default xml file
        /// </summary>
        public void LoadGameSettings()
        {
            if (File.Exists(fileName))
            {
                try
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(fileName);

                    XmlNodeList nodeList = document.GetElementsByTagName(TAG_NICKNAME);
                    if (nodeList.Count != 1) throw new Exception();
                    _nickname = nodeList[0].InnerText;

                    nodeList = document.GetElementsByTagName(TAG_SERVER_IP);                    
                    if (nodeList.Count != 1) throw new Exception();                    
                    _serverIP = nodeList[0].InnerText;
                    
                    nodeList = document.GetElementsByTagName(TAG_SERVER_PORT);
                    if (nodeList.Count != 1) throw new Exception();
                    _serverPort = Convert.ToInt32(nodeList[0].InnerText);

                    nodeList = document.GetElementsByTagName(TAG_LOCAL_PORT);
                    if (nodeList.Count != 1) throw new Exception();
                    _localPort = Convert.ToInt32(nodeList[0].InnerText);

                }
                catch (Exception)
                {
                    SaveGameSettings();
                }
            }
            else
            {
                SaveGameSettings();
            }
        }

        /// <summary>
        /// Saves the settings to the default xml file
        /// </summary>
        public void SaveGameSettings()
        {
            XmlDocument document = new XmlDocument();
            XmlElement rootElm = document.CreateElement(TAG_ROOT);

            document.AppendChild(rootElm);

            XmlElement node = document.CreateElement(TAG_NICKNAME);            
            node.InnerText = _nickname;
            rootElm.AppendChild(node);

            node = document.CreateElement(TAG_SERVER_IP);
            node.InnerText = _serverIP;
            rootElm.AppendChild(node);

            node = document.CreateElement(TAG_SERVER_PORT);
            node.InnerText = _serverPort + "";
            rootElm.AppendChild(node);

            node = document.CreateElement(TAG_LOCAL_PORT);
            node.InnerText = _localPort + "";
            rootElm.AppendChild(node);

            document.Save(fileName);
        }        

    }
}
